
var ResearchCenter = function(x, y, health, packetHealth, type, stage, callbackAtGoodPacketReception, callbackAtDie){
    var _shape;
    var _shapeHalo;
    var _x;
    var _y;
    var _type = type;
    var _callbackAtGoodPacketReception = callbackAtGoodPacketReception;
    var _callbackAtDie = callbackAtDie;
    var _waitingToBeProcessedPackets = new Array();
    var _connexion;
    var _maxHealth = health;
    var _health = health;
    var _image;
    var _imageHalo;
    var _txtHealthPoint;
    var _txtRemainingTime;
    var _packetHealth = packetHealth;
    
    /**
     * METHODS
     */
    this.getShape = function(){
        return _shape
    };
    this.getX = function(){
        return _x
    };
    this.getY = function(){
        return _y
    };
    this.getType = function(){
        return _type
    };
    
    this.setConnexion = function(connexion){
        _connexion=connexion;
    };
    
    this.getConnexion = function() {
        return _connexion;
    };
    
    this.getPacketHealth = function(){
        return _packetHealth;
    }
    
    this.decreaseHealth = function(damage){
        this.setHealth(_health-=damage);
    }
    this.getHealth = function(){
        return _health;
    }
    this.setHealth = function(h){
        if(h>_maxHealth){
            _health = _maxHealth;
        } else {
            _health = h;
        }
        
        if(_health<=0){
            _txtHealthPoint.text = 0;
            _callbackAtDie();
        } else {
            _txtHealthPoint.text = _health;
            
            if(_health > 66/100*_maxHealth){
                _txtHealthPoint.color = "#00CC22";
            } else if (_health > 33/100*_maxHealth){
                _txtHealthPoint.color = "orange";
            } else {
                _txtHealthPoint.color = "red";
            }
            
        }
    }
    //TODO - JR va apprendre à écrire une méthode en camelcase
    this.GetMaxHealth = function(){
        return _maxHealth;
    }
    
    this.sendPacket = function(speed, factory){
        var health = _packetHealth;
        var p = factory.getPacketWith(x, y, health, speed, this);
        //        var p = new Packet(this.getX(), this.getY(), health, speed, this, stage);
        var translation = new Translation(p, _connexion.getOppositePointFrom(this));
        this.getConnexion().addTranslation(translation);
        
        SoundEngine.getInstance().reseachCenterOut();
    };
    
    this.addPacketToWaitingList = function(packet){
        _waitingToBeProcessedPackets.push(packet);
    }
    
    this.processWaitingPacket = function(){
        var packet;
        for(packetIndex in _waitingToBeProcessedPackets){
            packet = _waitingToBeProcessedPackets[packetIndex];
            if(packet.getEmitter().getType() == _type || packet.getType() == 0){//if good type or colorless type
                if (packet.getEmitter() != this){
                    SoundEngine.getInstance().reseachCenterIn();
                    this.setHealth(this.getHealth()+packet.getHealth());
                    _callbackAtGoodPacketReception(packet.getHealth());
                }
            } else {
                SoundEngine.getInstance().reseachCenterWrong();
                this.setHealth(this.getHealth()-(packet.getHealth()/2));//Mauvais type - on enlève la moitiée de la vie du paquet
            }
            packet.destroy();
            _waitingToBeProcessedPackets.splice(packetIndex, 1);
        }
        
    };
    
    this.setRemainingTimeBeforeNewPacketSent = function(time){
        if(time > 0){
            _txtRemainingTime.text = Math.ceil(time/1000)+"s";
            if(!_shapeHalo.isVisible()){
                _shapeHalo.visible = true;
            }
        } else {
            _txtRemainingTime.text = "";
            _shapeHalo.visible = false;
        }
    //TODO affichage
    }
    
    /**
     * Return the path of the image to display.
     */
    this._getImageByType = function(type){
        // Check if the type passed can be called. (Call dynamiccaly to the method of ImageEngine)
        if(typeof ImageEngine.getInstance()['getCenterBase'+type] == 'function'){
            return ImageEngine.getInstance()['getCenterBase'+type]();
        }else{
            console.log("ResearchCenter->_getImageByType(): Le type fournit n'est pas un type attendu. Image par défaut utilisée.");
            return ImageEngine.getInstance().getDefaultCenterBase();
        }
    }
    
    /**
     * Return the path of the halo to display.
     */
    this._getImageHaloByType = function(type){
        // Check if the type passed can be called. (Call dynamiccaly to the method of ImageEngine)
        if(typeof ImageEngine.getInstance()['getHaloCenterBase'+type] == 'function'){
            return ImageEngine.getInstance()['getHaloCenterBase'+type]();
        }else{
            console.log("ResearchCenter->_getImageHaloByType(): Le type fournit n'est pas un type attendu. Halo par défaut utilisé.");
            return ImageEngine.getInstance().getHaloCenterBase1();
        }
    }
    
    /**
     * LOGIC
     */
    
    _shape = new createjs.Shape();
    _shapeHalo = new createjs.Shape();
    // Store positions.
    _x = x;
    _y = y;
    
    // Generate image.
    _image = this._getImageByType(_type);
    _imageHalo = this._getImageHaloByType(_type);
    

    // Center the element.
    _shape.x = _x - (_image.width/2)+45;
    _shape.y = _y - _image.height/2;
    
    _shapeHalo.x = _x - (_image.width/2)+45;
    _shapeHalo.y = _y - _image.height/2 - 5;

    // Generate the graphic element.
    _shape.graphics.beginBitmapFill(_image).drawRect(0, 0, _image.width, _image.height);
    _shapeHalo.graphics.beginBitmapFill(_imageHalo).drawRect(0, 0, _image.width, _image.height);
    
    // Add the shape.
    stage.addChildAt(_shape, 1);
    stage.addChildAt(_shapeHalo, 3);
    _shapeHalo.visible = false; 
    
    _txtHealthPoint = new createjs.Text(_health, "20px Verdana");
    _txtHealthPoint.x = _x+70;
    _txtHealthPoint.y = _y-27;//hauteur de l'image à la louche
    stage.addChild(_txtHealthPoint);
    
    //to set the right color
    this.setHealth(_maxHealth);
    
    _txtRemainingTime = new createjs.Text("", "16px Verdana", "red");
    _txtRemainingTime.x = _x+53;
    _txtRemainingTime.y = _y+5;
    stage.addChild(_txtRemainingTime);
        
}